package com.hi5.stexviewer.message.impl;

import static com.hi5.util.Validation.notEmpty;
import static com.hi5.util.Validation.state;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.beans.factory.InitializingBean;

import com.hi5.stexviewer.message.CallCenter;
import com.hi5.stexviewer.message.Customer;
import com.hi5.stexviewer.message.Message;
import com.hi5.stexviewer.message.MessageType;
import com.hi5.util.Validation;

public abstract class ThreadCustomer extends Thread implements Customer, InitializingBean {
    private Queue<Message<?>> messages = new ConcurrentLinkedQueue<Message<?>>();
    private CallCenter callCenter;

    public void recieve(Message<?> message) {
        if (MessageType.APPLICATION_START == message.getType()) {
            this.start();
        } else if (MessageType.APPLICATION_STOP == message.getType()) {
            this.interrupt();
        } else {
            messages.add(message);
        }
    }

    public void afterPropertiesSet() throws Exception {
        state(notEmpty(callCenter), "CallCenter must be set");
        callCenter.subscribe(this);
    }
    
    

    public CallCenter getCallCenter() {
        return callCenter;
    }

    public void setCallCenter(CallCenter callCenter) {
        this.callCenter = callCenter;
    }

    protected Queue<Message<?>> getMessages() {
        return messages;
    }
}
